package com.it.data_tree.util;

import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.*;

import java.awt.*;

import java.awt.image.BufferedImage;
import java.io.*;


@RestController
public class FileImg {
    public static void main(String[] args)  {
        createGif();
    }
    private static  void createGif(){
        String imgPath = "C:\\Users\\CYG\\Pictures\\Saved Pictures\\bird.gif";
        File source = new File(imgPath);
        String newFilePath = "C:\\Users\\CYG\\Pictures\\"+source.getName();

        try {
            zoomGifBySize(imgPath,200,200,newFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * GIF压缩尺寸大小
     * @param imagePath 原图片路径地址，如：F:\\a.png
     * @param width 目标文件宽
     * @param height 目标文件高
     * @param outputPath 输出文件路径（不带后缀），如：F:\\b，默认与原图片路径相同，为空时将会替代原文件
     * @throws IOException
     */
    public static void zoomGifBySize(String imagePath, int width, int height, String outputPath) throws IOException {
        // GIF需要特殊处理
        GifDecoder decoder = new GifDecoder();
        int status = decoder.read(imagePath);
        if (status != GifDecoder.STATUS_OK) {
            throw new IOException("read image " + imagePath + " error!");
        }
        // 拆分一帧一帧的压缩之后合成
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start(outputPath);
        encoder.setRepeat(decoder.getLoopCount());
        for (int i = 0; i < decoder.getFrameCount(); i++) {
            encoder.setDelay(decoder.getDelay(i));// 设置播放延迟时间
            BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流
            BufferedImage zoomImage = new BufferedImage(width, height, bufferedImage.getType());
            Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            Graphics gc = zoomImage.getGraphics();
            gc.setColor(Color.WHITE);
            gc.drawImage(image, 0, 0, null);
            encoder.addFrame(zoomImage);
        }
        encoder.finish();
        File outFile = new File(outputPath);
        BufferedImage image = ImageIO.read(outFile);
        ImageIO.write(image, outFile.getName(), outFile);
    }


    @RequestMapping("/gif")
    public ResponseEntity<byte[]> getGif() throws IOException {
        ColorPrintln.greenPrintln("请求成功！");
        createGif();
        BufferedImage image = ImageIO.read(new File("C:\\Users\\CYG\\Pictures\\bird.gif"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "gif", baos);
        byte[] imageData = baos.toByteArray();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_GIF);
        return new ResponseEntity<byte[]>(imageData, headers, HttpStatus.OK);
    }
}
